package websocket.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import websocket.dto.ResultDTO;
import websocket.dto.ResultEnum;
import websocket.entity.dto.ZhuanTaiDTO;
import websocket.entity.vo.SerialPortVo;
import websocket.exception_.*;
import websocket.listener.MyListener;
import websocket.service.ZhuanTaiService;
import websocket.util.ComBeanUtil;
import websocket.util.SerialTool;
import websocket.util.ZhuanTaiHexDataUtils;

@Service
public class ZhuanTaiServiceImpl implements ZhuanTaiService {

    //双轴转台
    @Value("${chaunKou.shuangZhouZhuanTaiName}")
    private String shuangZhouZhuanTaiName;
    //双轴转台
    @Value("${chaunKou.shuangZhouZhuanTaiPort}")
    private String shuangZhouZhuanTaiPort;

    @Override
    public ResultDTO zhuanTaiConnect(ZhuanTaiDTO resouce) {
        try {
            ComBeanUtil.getData();
            // 根据转台串口获取串口实例:先判断是否已打开串口，防止用户页面刷新后页面状态改变再次点击
            SerialPortVo serialPortVo = ComBeanUtil.getMap(resouce.getCkCode());
            // 判断是否已经打开
            if (serialPortVo != null) {
                return ResultDTO.success(null);
            }
            serialPortVo = SerialTool.openPort(shuangZhouZhuanTaiName, resouce.getCkCode(), resouce.getBtl());
            // 指令：通讯串口消息使用
            serialPortVo.sendData(ZhuanTaiHexDataUtils.tongXun());
            // 绑定监听
            serialPortVo.bindListener(new MyListener(), 500);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultDTO.success(null);
    }

    @Override
    public ResultDTO connectCancel(ZhuanTaiDTO resouce) {
        // 根据转台串口获取串口实例
        SerialPortVo serialPortVo = ComBeanUtil.getMap(resouce.getCkCode());
        if (serialPortVo == null) {
            return ResultDTO.fail(ResultEnum.FAIL_SERIAL);
        }
        ComBeanUtil.getData();
        ComBeanUtil.removeByKey(resouce.getCkCode());
        // 关闭串口连接
        serialPortVo.closePort2(serialPortVo);
        ComBeanUtil.getData();
        return ResultDTO.success(null);
    }

    @Override
    public ResultDTO runZhuanTai(ZhuanTaiDTO resouce) {
        // 横滚：1/俯仰：2/稳定：0（就是自动调平）
        try {
            // 根据转台串口获取串口实例
            SerialPortVo serialPortVo = ComBeanUtil.getMap(resouce.getCkCode());
            if (serialPortVo == null) {
                return ResultDTO.fail(ResultEnum.PLEASE_CONTENT);
            }
            byte[] runWaiHuan = ZhuanTaiHexDataUtils.runWaiHuan(resouce.getGzms(), resouce.getJd(), resouce.getSd(),resouce.getNine());
            // 串口数据发送
            serialPortVo.sendData(runWaiHuan);
        } catch (SendDataToSerialPortFailure sendDataToSerialPortFailure) {
            sendDataToSerialPortFailure.printStackTrace();
        } catch (SerialPortOutputStreamCloseFailure serialPortOutputStreamCloseFailure) {
            serialPortOutputStreamCloseFailure.printStackTrace();
        }
        return ResultDTO.success(null);
    }

    @Override
    public ResultDTO zhuanTaiShiNeng(ZhuanTaiDTO resouce) {
        // 根据转台串口获取串口实例
        SerialPortVo serialPortVo = ComBeanUtil.getMap(resouce.getCkCode());
        if (serialPortVo == null) {
            return ResultDTO.fail(ResultEnum.PLEASE_CONTENT);
        }
        byte[] sendData;
        // 1：内环、0外环
        if (resouce.getSnlx() == 1) {
            sendData = ZhuanTaiHexDataUtils.neiHuanShiNeng(1);
        } else {
            sendData = ZhuanTaiHexDataUtils.waiHuanShiNeng(1);
        }
        try {
            // 串口数据发送
            serialPortVo.sendData(sendData);
        } catch (SendDataToSerialPortFailure sendDataToSerialPortFailure) {
            sendDataToSerialPortFailure.printStackTrace();
        } catch (SerialPortOutputStreamCloseFailure serialPortOutputStreamCloseFailure) {
            serialPortOutputStreamCloseFailure.printStackTrace();
        }
        return ResultDTO.success(null);
    }

    @Override
    public ResultDTO zhuanTaiShiNengCancel(ZhuanTaiDTO resouce) {
        // 根据转台串口获取串口实例
        SerialPortVo serialPortVo = ComBeanUtil.getMap(resouce.getCkCode());
        if (serialPortVo == null) {
            return ResultDTO.fail(ResultEnum.FAIL_SERIAL);
        }
        byte[] sendData;
        // 1：内环、0外环
        if (resouce.getSnlx() == 1) {
            sendData = ZhuanTaiHexDataUtils.neiHuanShiNeng(0);
        } else {
            sendData = ZhuanTaiHexDataUtils.waiHuanShiNeng(0);
        }
        try {
            // 串口数据发送
            serialPortVo.sendData(sendData);
        } catch (SendDataToSerialPortFailure sendDataToSerialPortFailure) {
            sendDataToSerialPortFailure.printStackTrace();
        } catch (SerialPortOutputStreamCloseFailure serialPortOutputStreamCloseFailure) {
            serialPortOutputStreamCloseFailure.printStackTrace();
        }
        return ResultDTO.success(null);
    }
}
